package com.vnp.cooking.adapter;

import java.util.ArrayList;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.drawable.ColorDrawable;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.vnp.cooking.R;
import com.vnp.cooking.TungbuocthuchienActivity;
import com.vnp.cooking.UIcontrols.CustomImageView;
import com.vnp.cooking.UIcontrols.MyClickAbleImageView;
import com.vnp.cooking.UIcontrols.RounderImageView;
import com.vnp.cooking.bean.ObjBST;
import com.vnp.cooking.bean.ObjCachlam;
import com.vnp.cooking.utils.Utils;

public class ListCachlamSmallAdapter extends ArrayAdapter<ObjCachlam> {
	/**
	 * 
	 */
	Context context;
	ArrayList<ObjCachlam> objCachlams;
	ListCachlamSmallAdapter adapter = this;
	Dialog dialog;
	int positionItem;

	// thiet lap image loader
	DisplayImageOptions options = new DisplayImageOptions.Builder()
			.cacheInMemory(false).cacheOnDisc(true)
			.bitmapConfig(Config.RGB_565).build();

	public ListCachlamSmallAdapter(Context context,
			ArrayList<ObjCachlam> objCachlams) {
		super(context, R.layout.list_cach_lam_small_item, objCachlams);
		this.context = context;
		this.objCachlams = objCachlams;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public int getPosition(ObjCachlam item) {
		return super.getPosition(item);
	}

	@Override
	public ObjCachlam getItem(int position) {
		return objCachlams.get(position);
	}

	@Override
	public int getCount() {
		return objCachlams.size();
	}

	@Override
	public View getView(final int position, View v, ViewGroup parent) {
		Holder holder = null;
		if (v == null) {
			LayoutInflater inflater = LayoutInflater.from(getContext());
			v = inflater.inflate(R.layout.list_cach_lam_small_item, parent, false);

			holder = new Holder();

			holder.txtThutulam = (TextView) v
					.findViewById(R.id.item_list_thu_tu_buoc_lam);
			holder.txtMotabuoclam = (TextView) v
					.findViewById(R.id.item_list_txt_mota_tungbuoc);
			holder.imgBuoclam = (RounderImageView) v
					.findViewById(R.id.item_list_img_tungbuoc);
			holder.btnEdit = (ImageButton) v
					.findViewById(R.id.btn_edit_congthuc_dadang);
			holder.flImage = (FrameLayout) v.findViewById(R.id.fl_image);
			// holder.txtMotabuoclam.setSelection(holder.txtMotabuoclam.getText().toString().length());
			v.setTag(holder);

		} else {
			holder = (Holder) v.getTag();
		}
		// set gia tri
		holder.txtThutulam.setText("" + objCachlams.get(position).getStep());
		holder.txtMotabuoclam.setText(objCachlams.get(position)
				.getDecriptionStep());
		if (!objCachlams.get(position).getUrlImageStep().toString().matches("")
				&& !objCachlams.get(position).getUrlImageStep().toString()
						.matches("")) {
			ImageLoader.getInstance().displayImage(
					objCachlams.get(position).getUrlImageStep().toString(),
					holder.imgBuoclam, options);
//			setDataChange(objCachlams);
		} else {
			holder.flImage.setVisibility(View.GONE);
		}
		// ImageLoader.getInstance().displayImage("http://anuong.net/upload/recipe_img/12000/11062/keo-dua-hanh-nhan-phu-chocolate-sieu-ngon_50.jpg",
		// holder.imgBuoclam, options);


		if (position == (objCachlams.size() - 1)) {
			Animation anim = AnimationUtils.loadAnimation(getContext(),
					R.anim.gridview_fly_in_from_top_corner);
			v.setAnimation(anim);
			anim.start();
		} else {

		}

		return v;
	}

	// optimize
	class Holder {
		TextView txtThutulam;
		TextView txtMotabuoclam;
		RounderImageView imgBuoclam;
		ImageButton btnEdit;
		FrameLayout flImage;
	}

	public void setDataChange(ArrayList<ObjCachlam> objCachlams) {
		if (this.objCachlams != null)
			this.objCachlams = null;
		this.objCachlams = objCachlams;
		notifyDataSetChanged();
	}

	// show dialog chia se
	public void showDialogReport(final int positionItem) {
		dialog = new Dialog(context, R.style.DialogSlideAnim);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.setContentView(R.layout.dialog_edit_buoclam);

		// set cac phan tu ben trong
		Button btnXoabuoc = (Button) dialog.findViewById(R.id.btn_xoabuoc);
		Button btnChinhsua = (Button) dialog.findViewById(R.id.btn_suabuoclam);
		Button btnThoat = (Button) dialog.findViewById(R.id.btn_thoat);

		WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
		lp.copyFrom(dialog.getWindow().getAttributes());
		lp.width = WindowManager.LayoutParams.MATCH_PARENT;
		lp.height = WindowManager.LayoutParams.WRAP_CONTENT;

		dialog.setCanceledOnTouchOutside(true);
		dialog.show();
		dialog.getWindow().setAttributes(lp);
		dialog.getWindow().setGravity(Gravity.BOTTOM);
		dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));

		// setOnclick
		btnXoabuoc.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				dialog.dismiss();
				adapter.objCachlams.remove(positionItem);
				setDataChange(objCachlams);
			}
		});

		btnChinhsua.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog.dismiss();
				Intent t = new Intent(context, TungbuocthuchienActivity.class);
				t.putExtra("objCachlam", objCachlams.get(positionItem));
				getContext().startActivity(t);
			}
		});

		btnThoat.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog.dismiss();
			}
		});
	}

}
